Page({
  data: {
    title: '排行榜',
    userList: [{
        id: 1,
        avatar: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/avatar1.png',
        nickname: '用户1',
        energy: 99
      },
      {
        id: 2,
        avatar: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/avatar2.png',
        nickname: '用户2',
        energy: 4697
      },
      {
        id: 3,
        avatar: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/avatar3.png',
        nickname: '近宇化修近宇化修近宇化修',
        energy: 4698
      },
      {
        id: 4,
        avatar: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/avatar4.png',
        nickname: '用户4',
        energy: 4694
      },
      {
        id: 5,
        avatar: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/avatar5.png',
        nickname: '近宇化修近宇化修近宇化修近宇化修',
        energy: 4668
      },
      {
        id: 6,
        avatar: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/avatar6.png',
        nickname: '用户6',
        energy: 100
      },
      {
        id: 7,
        avatar: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/avatar7.png',
        nickname: '用户7',
        energy: 40
      },
      {
        id: 8,
        avatar: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/avatar8.png',
        nickname: '用户8',
        energy: 30
      },
      {
        id: 9,
        avatar: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/avatar9.png',
        nickname: '用户9',
        energy: 20
      },
      {
        id: 10,
        avatar: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/avatar10.png',
        nickname: '用户10',
        energy: 10
      }
    ],
    iconList: [{
        icon: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/icon/huodong.png',
        name: '活动',
        isAvailable: false,
        editable: false,
      },
      {
        icon: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/icon/jifenduihuan.png',
        name: '兑换',
        isAvailable: true,
        editable: false,
      },
      {
        icon: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/icon/jieshao.png',
        name: '介绍',
        isAvailable: false,
        editable: false,
      },
      {
        icon: 'cloud://tfjkp-0ge80vu9ea710148.7466-tfjkp-0ge80vu9ea710148-1305195523/myjkp/energyPage/icon/lipin.png',
        name: '礼品',
        isAvailable: false,
        editable: false,
      }
    ],
    exchangeList: [],
  },
  onLoad() {
    this.getProductList()
  },

  onUnload() {
    console.log("排行页面")
  },
  // 页面显示时触发（适用于从其他页面返回时）
  onShow() {
    console.log('页面显示 onShow');
    this.getProductList();
  },
  onLoad() {
    console.log('页面加载 onLoad')
    this.getProductList()
  },
  // 获取产品列表
  getProductList: async function () {
    try {
      const res = await apiService.wxRequestPromise({
        url: urlProductList,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'charset': 'UTF-8'
        },
      });

      console.log("[energy.home] 后端返回的数据:", res);

      // 检查返回数据的结构，假设是 res.data.list
      if (res && res.data && Array.isArray(res.data.list)) {
        this.setData({
          exchangeList: res.data.list // 将获取到的产品列表赋值给 exchangeList
        });
        console.log("[energy.home] exchangeList 更新:", this.data.exchangeList);
      } else {
        wx.showToast({
          title: '未获取到商品列表',
          icon: 'none',
          duration: 2000
        });
      }

    } catch (err) {
      console.error("[energy.home] 请求失败:", err);
      wx.showToast({
        title: '加载商品列表失败，请稍后再试',
        icon: 'none',
        duration: 2000
      });
    }
  },
});